/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.mtasc;

import java.io.File;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * Implements all primitives from 'zlibstubs.c'.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class STuff {
	
    /**
     * No instance of this class.
     */
    private STuff() {
    } // end empty constructor

	@Primitive
	public static Value get_full_path(final CodeRunner ctxt, final Value f)
		throws Fail.Exception {
/*		try{
			String fullPathString = new File(f.toString()).getAbsolutePath();
			Block fullPathBlock = Block.createString(fullPathString.getBytes());
			return Value.createFromBlock(fullPathBlock);
		} catch (Exception e){
			error(ctxt, "STuff.get_full_path", e);
			return Value.UNIT;
		}*/
		
		error(ctxt, "STuff.get_full_path", new Exception("salut nounou"));
		return Value.UNIT;
	}
	
	@Primitive
	public static Value executable_path(final CodeRunner ctxt, final Value f)
		throws Fail.Exception {
/*		try{
			String exePathString = System.getEnv("_");
			Block fullPathBlock = Block.createString(exePathString.getBytes());
			return Value.createFromBlock(fullPathBlock);
		} catch (Exception e){
			error(ctxt, "STuff.executable_path", e);
			return Value.UNIT;
		}*/
		
		error(ctxt, "STuff.get_full_path", new Exception("salut nounou"));
		return Value.UNIT;		
	}	
	
    /**
     * Raises a <i>Zlib.Error</i> exception.
     * @param ctxt context - should not be <tt>null</tt>
     * @param prim primitive raising exception - should not be <tt>null</tt>
     * @param e caught Java exception - should not be <tt>null</tt>
     * @throws Fail.Exception always
     */
    private static void error(final CodeRunner ctxt,
                              final String prim,
                              final Exception e)
        throws Fail.Exception {
        assert ctxt != null : "null ctxt";
        assert prim != null : "null prim";
        assert e != null : "null e";
        final Value mtascException =
            ctxt.getContext().getCallback("STuff.Error");
        if (mtascException == null) {
            Fail.invalidArgument("Exception STuff.Error not initialized");
        } else {
            final String msg = e.getMessage();
            final Block bucket =
                Block.createBlock(0,
                                  mtascException,
                                  Value.createFromBlock(Block.createString(prim)),
                                  Value.createFromBlock(Block.createString(msg != null
                                                                           ? msg
                                                                           : "")));
            Fail.raise(Value.createFromBlock(bucket));
        } // end if/else
    } // end method 'error(CodeRunner, String, Exception)'	

} // end class 'STuff'
